<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <!-- react容器 -->
    <div id="test"></div>

    <!-- 引入react核心库 -->
    <script src="../lib/react.js"></script>
    <!-- 引入react-dom -->
    <script src="../lib/react-dom.js"></script>
    <!-- 引入babel -->
    <script src="../lib/babel.min.js"></script>
    <script type="text/babel">
      //创建组件
      class Life extends React.Component {
        state = {
          opacity: 1,
        };
        sleep = () => {
          ReactDOM.unmountComponentAtNode(document.getElementById("test"));
        };

        //组件挂载完毕
        componentDidMount() {
          this.timer = setInterval(() => {
            let { opacity } = this.state;
            opacity -= 0.2;
            if (opacity <= 0) {
              opacity = 1;
            }
            this.setState({ opacity });
          }, 200);
        }
        //组件将要卸载
        componentWillUnmount() {
          //清除定时器
          clearInterval(this.timer);
        }
        render() {
          return (
            <div>
              <h2 style={{ opacity: this.state.opacity }}>学不会怎么办</h2>
              <button onClick={this.sleep}>睡觉</button>
            </div>
          );
        }
      }
      ReactDOM.render(<Life />, document.getElementById("test"));
    </script>
  </body>
</html>
